<?php
/**
 * 权限组管理
 * @author chenlisong E-mail:chenlisong1021@163.com 
 * @version 创建时间：2015年12月29日 下午3:16:58 
 * 类说明
 */
class AuthGroupAction extends BaseAction{
	/**
	 * @authority 浏览
	 */
	public function index(){
		$this->display('index');
	}
	
	//列表
	public function getAllData(){
		$Model = M('Auth_group');
		$map = $this->loadSeachCondition();
		$map['status'] = array('neq',-1);
		$total = $Model->where($map)->count();// 查询满足要求的总记录数
		$page = json_decode($this->pageParam($total));
		$data = $Model->where($map)->limit($page->firstRow,$page->listRows)->order($page->sort)->select();
		$vo = $this->toJosnForGird($data,$page);
		echo json_encode($vo);
	}
	
	/**
	 * 组装条件
	 * @return multitype:multitype:string
	 */
	public function loadSeachCondition(){
		$map = array();
		/*
		if(!empty($_GET['field'])){
			$map['field'] = array("like","%".$_GET['field']."%");
		}
		*/
		return $map;
	}
	
	/**
	 * @authority 设置权限
	 */
	public function config(){
		$Model = M('Auth_rule');
		$GroupModel = M('Auth_group');
		$map['status'] = 1;
		$map['type'] = 1;
		$groupWhere['id'] = $_REQUEST["id"];
		$groupWhere['status'] = array('neq',-1);
		$result = $GroupModel->where($groupWhere)->find();
		$rules = explode(',',$result['rules']);
		$vo = $Model->where($map)->order('seq asc')->select();
		//查询出层级关系数据
		for($i=0; $i<count($vo); $i++) {
			$map['type'] = 2;
			$map['parent_id'] = $vo[$i]['id'];
			$sub_menu = $Model->where($map)->select();
			$vo[$i]['sub_menu'] = $sub_menu;
			for($j=0; $j<count($sub_menu); $j++) {
				$map['type'] = 3;
				$map['parent_id'] = $sub_menu[$j]['id'];
				$sub_method = $Model->where($map)->select();
				$vo[$i]['sub_menu'][$j]['sub_method'] = $sub_method;
			}
		}
		//判断是否拥有该权限
		for($a=0;$a<count($vo);$a++) {
			for($b=0;$b<count($vo[$a]["sub_menu"]);$b++) {
				if(in_array($vo[$a]["sub_menu"][$b]["id"],$rules)) {
					$vo[$a]["sub_menu"][$b]["isCheck"] = 0;
				}else{
					$vo[$a]["sub_menu"][$b]["isCheck"] = 1;
				}
				for($c=0;$c<count($vo[$a]["sub_menu"][$b]["sub_method"]);$c++) {
					if(in_array($vo[$a]["sub_menu"][$b]["sub_method"][$c]["id"],$rules)) {
						$vo[$a]["sub_menu"][$b]["sub_method"][$c]["isCheck"] = 0;//选中
					}else{
						$vo[$a]["sub_menu"][$b]["sub_method"][$c]["isCheck"] = 1;//不选中
					}
				}
			}
		}
		$this->assign('id',$groupWhere['id']);
		$this->assign('vo',$vo);
		$this->display();
	}
	
	//更新权限
	public function updateRules(){
		$Model = M('Auth_group');
		$map['id'] = $_REQUEST['id'];
		$data['rules'] = $_REQUEST['rules'];
		if (false !== $Model->where($map)->save($data)) {
			$this->ajaxReturn(1,L('ADMIN_UPDATE_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_UPDATE_ERROR'),0);
		}
	}
	
	/**
	 * 更改状态
	 */
	public function updateStatus() {
		//删除指定记录
		$Model = M($this->getActionName());
		$GroupAccessModel = M("Auth_group_access");
		$pk = $Model->getPk();
		$id = $_REQUEST[$pk];
		$where['group_id'] = $id;
		$count = $GroupAccessModel->where($where)->count();
		if($count > 0){
			$this->ajaxReturn(0,"该权限组已被管理员绑定，请删除后操作！",0);
		}
		if (isset($id)) {
			$condition = array($pk => array('in', explode(',', $id)));
			if (false !== $Model->where($condition)->setField($_REQUEST['field'],$_REQUEST['islock'])) {
				$this->ajaxReturn(1,L('ADMIN_EDIT_SUCCESS'),1);
			} else {
				$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
			}
		}
	}
	
}